<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('运营数据')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>创建时间：</label>
                            <input type="text" class="time-input" placeholder="请选择创建时间" id="startDate" name="startDate"
                                   autocomplete="off">
                            -
                            <input type="text" class="time-input" placeholder="请选择创建时间" id="endDate" name="endDate"
                                   autocomplete="off"/>
                        </li>
                        <li>
                            <label>用户：</label>
                            <select name="userId" id="userId" th:with="users=${@sysuser.getAllUsers()}"
                                    class="form-control select2-multiple" multiple>
                                <option value="">全部</option>
                                <option th:each="user : ${users}" th:text="${user.key}"
                                        th:value="${user.value}"></option>
                            </select>
                        </li>
                        <li>
                            <label>药品：</label>
                            <select name="drugId" id="drugId" th:with="drags=${@sysuser.getAllDrags()}"
                                    class="form-control select2-multiple" multiple>
                                <option value="">全部</option>
                                <option th:each="drag : ${drags}" th:text="${drag.key}"
                                        th:value="${drag.value}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                            <a class="btn btn-info btn-rounded btn-sm" onclick="getDaily()"><i
                                    class="fa fa-book"></i>日报</a>
                            <a class="btn btn-info btn-rounded btn-sm" onclick="getMonthlyReport()"><i
                                    class="fa fa-book"></i>月报</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:src="@{/js/common.js}"></script>
<th:block th:include="include :: select2-css"/>
<th:block th:include="include :: select2-js"/>
<script th:inline="javascript">
    var prefix = ctx + "system/operation";

    $(function () {
        var options = {
            url: prefix + "/getSalesData",
            modalName: "处方信息",
            pageSize: 1000,
            columns: [
                {
                    field: 'user_name',
                    title: '角色名',
                    visible: true,
                    sortable: true,
                },
                {
                    field: 'amount',
                    title: '销售额',
                    sortable: true,
                },
                {
                    field: 'profit',
                    title: '利润',
                    sortable: true,
                }
            ],
            onPostBody: function (data) {
                let t1 = 0, t2 = 0;
                data.forEach(a => {
                    t1 += a.amount;
                    t2 += a.profit;
                })
                let html = `<span style="font-size: 1.4em;font-weight: bold">总销售额：${t1}，总利润：${t2}</span>`
                $(".fixed-table-toolbar .bs-bars").html(html);
            }
        };
        $.table.init(options);
    });

    function mysearch() {
        $.table.search()
    }

    function getDaily() {
        $.ajax({
            url: prefix + "/getDaily",
            type: 'post',
            // contentType: 'application/x-www-form-urlencoded',
            data: {
                startDate: $("#startDate").val(),
                endDate: $("#endDate").val(),
            },
            dataType: 'json',
        }).then((res) => {
            if(res.msg === '操作成功'){
                $.modal.alertWarning("日报生成成功");
                $.modal.enable();
                $.modal.closeLoading();
            }
        })
    }


    function getMonthlyReport() {
        $.ajax({
            url: prefix + "/getMonthlyReport",
            type: 'post',
            // contentType: 'application/x-www-form-urlencoded',
            data: {
                startDate: $("#startDate").val(),
                endDate: $("#endDate").val(),
            },
            dataType: 'json',
        }).then((res) => {
            if(res.msg === '操作成功'){
                $.modal.alertWarning("月报生成成功");
                $.modal.enable();
                $.modal.closeLoading();
            }
        })
    }
</script>
</body>
</html>